package com.tgy.studentmanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.tgy.studentmanagement.VO.QueryStudentVo;
import com.tgy.studentmanagement.common.R;
import com.tgy.studentmanagement.common.RPage;
import com.tgy.studentmanagement.dao.StudentsDao;
import com.tgy.studentmanagement.entity.Students;
import com.tgy.studentmanagement.service.StudentsService;
import com.tgy.studentmanagement.utils.MinIoUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * (Students)表控制层
 *
 * @author
 * @since 2023-04-12 14:14:17
 */
@RestController
@RequestMapping("students")
@RequiredArgsConstructor
@Slf4j
public class StudentsController {
    private final StudentsService studentsService;
    private final StudentsDao studentsDao;
    private final RestTemplate restTemplate;
    private final MinIoUtil minIoUtil;

    /**
     * 编辑数据
     *
     * @param students 实体
     * @return 编辑结果
     */
    @PutMapping("/upd")
    @ApiOperation("编辑学生信息")
    public R edit(@Validated @RequestBody Students students) {
        this.studentsService.update(students);
        return R.ok();
    }

    /**
     * 删除数据
     *
     * @param studentId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/del/{studentId}")
    @ApiOperation("删除学生信息")
    public R ResultVodeleteById(@PathVariable String studentId) {
        boolean b = this.studentsService.deleteByStudentId(studentId);
        if (b) {
            return R.ok();
        } else {
            return R.failed();
        }

    }


    /**
     * 新增数据
     *
     * @param students 实体
     * @return 新增结果
     */
    @ApiOperation("添加学生信息")
    @PostMapping("/add")
    public R add(@Validated @RequestBody Students students) {
        return R.ok(this.studentsService.insert(students));
    }


    /**
     * 查询学生信息
     *
     * @param queryStudentVo
     * @return
     */
    @PostMapping("/query")
    @ApiOperation("查询学生信息")
    public R<List<Students>> ResultVodeleteById(@RequestBody QueryStudentVo queryStudentVo) {
        this.studentsService.query(queryStudentVo).get(0).toString();
        return R.ok(this.studentsService.query(queryStudentVo));

    }

    @GetMapping("/getStatisticInfo")
    @ApiOperation("查询统计信息")
    public R getStatisticInfo() {
        log.info("查询统计信息");
        return R.ok(this.studentsService.getStatisticInfo());

    }

    @ApiOperation("从数据库导出学生信息")
    @RequestMapping(value = "/exportStudentsList", method = RequestMethod.GET)
    public R exportMemberList(HttpServletRequest request, HttpServletResponse response) {
        this.studentsService.exportMemberList(request, response);
        return R.ok();
    }


    @ApiOperation("从Excel导入学生信息")
    @RequestMapping(value = "/importStudentsList", method = RequestMethod.POST)
    @ResponseBody
    public R importMemberList(@RequestPart("file") MultipartFile file) {

        return R.ok(this.studentsService.importMemberList(file));
    }


    @GetMapping("/getByPage")
    @ApiOperation("分页查询")
    public RPage getByPage(int pageSize, int pageIndex) {

        log.info("查询统计信息");
        Page<Object> objects = PageMethod.startPage(pageIndex, pageSize);
        LambdaQueryWrapper<Students> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Students::getAge, 8);
        List<Students> students = this.studentsDao.selectList(wrapper);
        return RPage.page(students, objects.getTotal());
    }


    @GetMapping("/testZip")
    public void testZip(HttpServletResponse response) {
        String url1 = "http://134.84.202.100:10028/aj-report/f660ccf7-66fb-480f-be53-92f80aa7cba2.jpg";
        String url2 = "http://134.84.202.100:10028/aj-report/fbfe9cf7-8ee0-49a7-8c93-0cbb900b8165.png";


        String forObject = this.restTemplate.getForObject(url1, String.class);
        log.info(forObject);
        MinIoUtil.download("aj-report", "fbfe9cf7-8ee0-49a7-8c93-0cbb900b8165.png", response);

//        ArrayList<String> fileList = new ArrayList<>();
//        //文件路径
//        fileList.add("././license/key/private.key");
//        fileList.add("././license/key/public.key");
//        ZipFileUtils.downloadZipFiles(response,fileList,"test.zip");
    }


    /*
     */


    @GetMapping("/files")
    public void downloadFiles(@RequestParam("bucket") String bucket,
                              @RequestParam("fileNames") List<String> fileNames,
                              HttpServletResponse response) throws IOException {
        minIoUtil.download("aj-report", fileNames, response);

    }


    /**
     * 这里前端可以自己压缩多个zip
     * @param bucket
     * @param fileNames
     * @param response
     * @throws IOException
     */
    @GetMapping("/testFiles")
    public void testFiles(@RequestParam("bucket") String bucket,
                              @RequestParam("fileNames") List<String> fileNames,
                              HttpServletResponse response) throws IOException {
        minIoUtil.downloadFiles("aj-report", fileNames, response);

    }


}

